package com.example.gmallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.gmallproduct.VO.AttrResponseVo;
import com.example.gmallproduct.VO.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.gmallproduct.entity.AttrEntity;
import com.example.gmallproduct.service.AttrService;
import com.common.utils.PageUtils;
import com.common.utils.R;



/**
 * 商品属性
 *
 * @author WDL
 * @email 2329156214@QQ.com
 * @date 2022-05-01 15:44:58
 */
@RestController
@RequestMapping("gmallproduct/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

//    /**
//     * 被下方的方法代替了
//     * @param params
//     * @param catelogId
//     * @return
//     */
//    @RequestMapping("/sale/list/{catelogId}")
//    public R saleList(@RequestParam Map<String,Object> params,@PathVariable("catelogId")Long catelogId){
//
//        PageUtils page = attrService.getBySaleList(params, catelogId);
//
//        return R.ok().put("page",page);
//    }


    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catId, @PathVariable("attrType") String attrType) {

        PageUtils page = attrService.queryPageByCatId(params, catId, attrType);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
//		AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo detail = attrService.findDetail(attrId);
        return R.ok().put("attr", detail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveCaseCade(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr) {
//		attrService.updateById(attr);
        attrService.updateCaseCade(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
